from control.models import *
from literature.models import *
import pandas as pd
from django.db import IntegrityError, transaction
import json
from tqdm import tqdm
import traceback
from datetime import datetime


def init_literature_data():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()
    pub_cycle, _ = JournalPublicationCycleType.objects.get_or_create(
        name="unknown",
        creator=super_admin_entity,
        last_editor=super_admin_entity,
    )
    status = LiteratureStatusType.objects.filter(name="Available")[0]
    # liter_type = LiteratureType.objects.filter(name="Unspecified")[0]
    # inst_type = InstitutionType.objects.filter(name="unknown")[0]
    unit_file_path = "files/basic_data_init_final_v2/A075. PCdata基表-3-参考文献表（20250403R1）-0版.xlsx"
    # unit_file_path = "files/basic_data_init_final_v1/基表20250306/A075test.xlsx"
    sheet_name = "文献总表"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=1, dtype=str)
    start_row = 1
    end_row = 124030
    # end_row = 13
    num_columns = 43
    selected_data = df.iloc[start_row:, :num_columns].fillna("")
    
    # print("selected_data",selected_data)
    # print(selected_data.columns)
    # return 

    block_size = 1000

    for index, row in tqdm(selected_data.iterrows(), desc="Processing literature data"):
        try:
            with transaction.atomic():
                if index < -1:
                    continue
                nPCdataCitID = row["nPCdataCitID"]
                eCitationType = row["eCitationType"].title()
                if eCitationType == "":
                    eCitationType_entity, _ = LiteratureType.objects.get_or_create(
                        name="Unspecified",
                        creator=super_admin_entity,
                        last_editor=super_admin_entity,
                    )
                else:
                    eCitationType_entity, _ = LiteratureType.objects.get_or_create(
                        name=eCitationType,
                        creator=super_admin_entity,
                        last_editor=super_admin_entity,
                    )
                sAuthorList = row["sAuthorList"].split(";")
                author_entity_list = []
                for author_en in sAuthorList:
                    if author_en != "" and len(author_en) < 128:
                        author, _ = Author.objects.get_or_create(
                            name=author_en,
                            creator=super_admin_entity,
                            last_editor=super_admin_entity,
                        )
                        author_entity_list.append(author)

                year = row["yrPubYr"]
                if type(year) != int:
                    if type(year) == str and year.isdigit():
                        year = int(year)
                    else:
                        year = None
                date = (
                    None if year is None or year <= 0 else datetime(year, 1, 1).date()
                )
                sTitle = row["sTitle"]
                sAbstract = row["sAbstract"].strip()
                eLanguage = row["eLanguage"]
                if eLanguage == "":
                    language_entity, _ = Language.objects.get_or_create(
                        name="unknown",
                        creator=super_admin_entity,
                        last_editor=super_admin_entity,
                    )
                else:
                    language_entity, _ = Language.objects.get_or_create(
                        name=eLanguage,
                        creator=super_admin_entity,
                        last_editor=super_admin_entity,
                    )
                sDOI = row["sDOI"]
                urlCit = row["urlCit"]
                vol = None if row["sVol"] == "" else row["sVol"]
                sPage = row["sPage"]
                if sPage == "":
                    first_page = None
                    last_page = None
                else:
                    if "-" in sPage:
                        pages = sPage.split("-")
                        first_page, last_page = pages[0], pages[1]
                        if not first_page.isdigit():
                            first_page=None
                        if not last_page.isdigit():
                            last_page=None
                        if first_page and len(first_page)>6:
                            first_page=None
                        if last_page and len(last_page)>6:
                            last_page=None
                        
                    else:
                        first_page = sPage
                        last_page = sPage
                        if not first_page.isdigit():
                            first_page=None
                        if not last_page.isdigit():
                            last_page=None
                        if first_page and len(first_page)>6:
                            first_page=None
                        if last_page and len(last_page)>6:
                            last_page=None
                sJIssue = row["sJIssue"]
                sPubName = row["sPubName"]
                if sPubName == "":
                    publication_entity = None
                else:
                    # 对一些类型进行处理
                    if eCitationType == "Book":
                        book_entity = Book.objects.filter(name=sPubName).first()
                        if book_entity == None:
                            sBISBN = row["sBISBN"]
                            sBEditior = row["sBEditior"].split(";")
                            book_author_list = []
                            for auth_name in sBEditior:
                                anth_entity, _ = Author.objects.get_or_create(
                                    name=auth_name,
                                    creator=super_admin_entity,
                                    last_editor=super_admin_entity,
                                )
                                book_author_list.append(anth_entity)
                            book_entity = Book.objects.create(
                                name=sPubName,
                                isbn=sBISBN,
                                language=language_entity,
                                creator=super_admin_entity,
                                last_editor=super_admin_entity,
                            )
                            book_entity.authors.set(book_author_list)
                        publication_entity = book_entity
                    elif eCitationType == "Journal":
                        journal_entity = Journal.objects.filter(name=sPubName).first()
                        if journal_entity == None:
                            sJISSN = row["sJISSN"]
                            journal_entity = Journal.objects.create(
                                name=sPubName,
                                issn=sJISSN,
                                publication_cycle=pub_cycle,
                                creator=super_admin_entity,
                                last_editor=super_admin_entity,
                            )
                        publication_entity = journal_entity
                    else:
                        

                        publication_entity = Publication.objects.filter(
                            name=sPubName
                        ).first()
                        if publication_entity==None:
                            publication_entity = Publication.objects.create(
                                name=sPubName,
                                creator=super_admin_entity,
                                last_editor=super_admin_entity,
                            )
                        

                cit_name = "SEI"
                extra_json = {}
                if row["DETHERM_ID"] != "" and row["DETHERM_ID"] != "NA":
                    extra_json["DETHERM_ID"] = row["DETHERM_ID"]
                if row["TDE_ID"] != "" and row["TDE_ID"] != "NA":
                    extra_json["TDE_ID"] = row["TDE_ID"]
                if row["DIPPR_ID"] != "" and row["DIPPR_ID"] != "NA":
                    extra_json["DIPPR_ID"] = row["DIPPR_ID"]
                pub_type = "unknown"
                if eCitationType=="Book":
                    pub_type="Book"
                elif eCitationType=="Journal":
                    pub_type="Journal"
                
                literature_entity = Literature.objects.create(
                    title=sTitle,
                    zh_title=sTitle,
                    date=date,
                    abstract=sAbstract,
                    language=language_entity,
                    DOI=sDOI,
                    pub_type=pub_type,
                    status=status,
                    liter_code=nPCdataCitID,
                    net_src=urlCit,
                    liter_type=eCitationType_entity,
                    pub_id=publication_entity,
                    volume=vol,
                    issue=sJIssue,
                    first_page=first_page,
                    last_page=last_page,
                    cit_name=cit_name,
                    creator=super_admin_entity,
                    last_editor=super_admin_entity,
                    extra_properties=extra_json,
                )
                literature_entity.authors.set(author_entity_list)
                literature_entity.save()
        except Exception as e:
            traceback.print_exc()
            print("error at index:", index)
            print("error at id:", row["nPCdataCitID"])

            return


def init_literature_content():
    init_literature_data()

    print("finish literature")
